package com.xiaomiMall.mange.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.xiaomiMall.mange.domain.XiaoMiProd;
import com.xiaomiMall.mange.domain.XiaoMiProdDetail;
import com.xiaomiMall.mange.service.MallUserService;
import com.xiaomiMall.mange.service.XiaoMiProdService;
import com.xiaomiMall.mange.vo.XiaoMiProdandDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author codefish
 * @date 2021/11/2
 * @apinote
 */
@RestController
@RequestMapping("/api/shopping/list")
public class ShoppingCartController{

    @Autowired
    private XiaoMiProdService xiaoMiProdService;
    @Autowired
    private MallUserService mallUserService;
    private String jsonString;
    private String colorJson;

    @RequestMapping("/getProdList")
    public String getProdInfo(XiaoMiProd xiaoMiProd){
        try {
            //1.new ObjectMapper()
            ObjectMapper mapper = new ObjectMapper();
            XiaoMiProd xiaoMiProds = xiaoMiProdService.getProdList(xiaoMiProd).get(0);

            List<XiaoMiProdDetail> xiaoMiProdDetailList = xiaoMiProdService.getProdDetailList(xiaoMiProd);

            XiaoMiProdandDetailVo xiaoMiProdandDetailVo = new XiaoMiProdandDetailVo();
            xiaoMiProdandDetailVo.setXiaoMiProd(xiaoMiProds);
            //jackson库: 把对象转换成json
            xiaoMiProdandDetailVo.setXiaoMiProdDetailList(xiaoMiProdDetailList);
            //对象转换成json
            jsonString = mapper.writeValueAsString(xiaoMiProdandDetailVo);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return jsonString;
    }

    @RequestMapping("getProdColor")
    public String getProdColor(String uid, String prodId){

        try {
            List colorList =  xiaoMiProdService.getProdColor(uid, prodId);
            ObjectMapper mapper = new ObjectMapper();
            colorJson = mapper.writeValueAsString(colorList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return colorJson;
    }

    @PostMapping("updateNickName")
    public AjaxResult updateNickName(String userName, String userEmail){
        boolean result = mallUserService.updateNickName(userName, userEmail);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}